/*
 * @lc app=leetcode.cn id=859 lang=cpp
 *
 * [859] 亲密字符串
 */

// @lc code=start
class Solution
{
public:
  bool buddyStrings(string s, string goal)
  {
    int m = s.size(), n = goal.size();
    if (m != n) return false;
    multiset<int> a, b;
    map<int, int> mp;
    for (int i = 0; i < m; i++) {
      mp[s[i]]++;
      if (mp[s[i]] == 2 && s == goal) return true;
      if (s[i] != goal[i]) {
        a.insert(s[i]);
        b.insert(goal[i]);
      }
    }
    if (a == b && a.size() == 2) return true;
    return false;
  }
};
// @lc code=end
